<?php

$this->LoadHelper('MemberHelper');
$MemberHelper = new MemberHelper();

$this->LoadHelper('OrderHelper');
$OrderHelper = new OrderHelper();

$member_info = $MemberHelper->getMember($this->UserConfig['Id']);
$this->Assign('member_info', $member_info);
if (empty($this->Param['option'])) {    
    //订单列表
    $ajax = $this->Param['ajax'];
    $page = isset($this->Param['page']) ? $this->Param['page'] : 1;
    $rp = isset($this->Param['rp']) ? $this->Param['rp'] : PAGE_SIZE;
    if ($ajax) {
        if($ajax==1 || !empty($this->Param['o_keyword'])) {
            $where = array(
                '`o_status` NOT IN(?)' => array('征信提交', '征信查询', '作废', '退回')
            );
            $data = $OrderHelper->orderList($where, $page, $rp, $this->Param, $member_info, 'order_upview', array('o_suspend_status DESC', 'total DESC'));
            $json_data = array('total' => $data['count'], 'rows' => array());
            if (!empty($data['all']))
                foreach ($data['all'] AS $rowNum => $row) {
                    //统计申请未处理条数
                    $undetial_num = $OrderHelper->orderupCount(array(
                        '`up_oid` = ?' => $row['o_id'],
                        '`up_status` = ?' => '申请中'
                    ));
                    $row['undetail'] = empty($undetial_num) ? 0 : $undetial_num;
                    $o_perch = $MemberHelper->getMemberRow($row['o_final_perch']);
                    $row['o_final_perch_name'] = $o_perch['u_realname'];

                    $o_video_perch = $MemberHelper->getMemberRow($row['o_videoaudit_perch']);
                    $row['o_videoaudit_perch_name'] = $o_videoaudit_perch['u_realname'];
                    $row['o_suspend_remarks'] = '';
                    if ($row['o_layer_status'] == '暂停中') {
                        $audit_msg = $OrderHelper->getOneAudit($row['o_id'], 'fao');
                        if (!empty($audit_msg)) $row['o_suspend_remarks'] = $row['o_suspend_remarks'] . "电审【" . $row['o_final_perch_name'] . "】于" . $audit_msg['a_dateline'] . "暂停审核,原因为:\n" . $audit_msg['a_remarks'] . "\n";
                    }
                    if ($row['o_trial_status'] == '暂停中') {
                        $audit_msg = $OrderHelper->getOneAudit($row['o_id'], 'fto');
                        if (!empty($audit_msg)) $row['o_suspend_remarks'] = $row['o_suspend_remarks'] . "资审【" . $row['o_perch_name'] . "】于" . $audit_msg['a_dateline'] . "暂停审核,原因为:\n" . $audit_msg['a_remarks'] . "\n";
                    }
                    if ($row['o_videoaudit_status'] == '暂停中') {
                        $audit_msg = $OrderHelper->getOneAudit($row['o_id'], 'fvo');
                        if (!empty($audit_msg)) $row['o_suspend_remarks'] = $row['o_suspend_remarks'] . "视频审" . $row['o_videoaudit_perch_name'] . "】于" . $audit_msg['a_dateline'] . "暂停审核,原因为:\n" . $audit_msg['a_remarks'] . "\n";
                    }
                    $row['o_suspend_remarks'] = $row['o_suspend_remarks'] . "如需继续审核请及时使用“订单申请修改”功能！";
                    $json_data['rows'][] = $row;
                }
            echo json_encode($json_data);
        }else{
            $table = 'order_upding';
            $where['`o_status` != ?'] = '作废';
            if (!empty($param['o_keyword'])) {
                $where['LOCATE (?,`o_vip_name`)  > 0'] = $param['o_vip_name'];
            }
            if (!empty($param['o_add_time_st'])) {
                $where['`o_add_time` >= ?'] = $param['o_add_time_st'];
            }
            if (!empty($param['o_add_time_et'])) {
                $where['`o_add_time` < ?'] = $param['o_add_time_et'];
            }

            if($member_info['u_dp_type'] == 'dot'){
                if($member_info['u_scope'] == '用户级'){
                    $where['`o_branch_type` = "dot" AND `o_uid` = ?'] = $member_info['u_id'];
                }
                if($member_info['u_scope'] == '部门级'){
                    $where['`o_branch_type` = "dot" AND `o_branch_id` = ?'] = $member_info['u_dp_id'];
                }
            }

            if($member_info['u_role'] == 8) {//电审
                //$table = 'order_upaing';
                $table = 'order_upding';
                $where['`o_final_perch` = ?'] = $member_info['u_id'];
            }

            if($member_info['u_role'] == 7){//资审
                //$table = 'order_upting';
                $table = 'order_upding';
                $where['`o_perch` = ?'] = $member_info['u_id'];
            }
            if($member_info['u_role'] == 10){//视频审
                $table = 'order_upding';
                $where['`o_videoaudit_perch` = ?'] = $member_info['u_id'];
            }
            if($member_info['u_role'] == 4){//大区业务员
                $table = 'order_upding';
                $where['`br_uid` = ?'] = $member_info['u_id'];
            }
            $data = $this->LoadModel('Public')->getList($table, '*', $where, array($page, $rp), null, array('o_suspend_status DESC', 'undetail DESC'));
            $json_data = array('total' => 0, 'rows' => array());
            if (!empty($data))
                $json_data = array('total' => count($data), 'rows' => array());
                foreach ($data AS $rowNum => $row) {
                    $o_perch = $MemberHelper->getMemberRow($row['o_final_perch']);
                    $row['o_final_perch_name'] = $o_perch['u_realname'];

                    $o_video_perch = $MemberHelper->getMemberRow($row['o_videoaudit_perch']);
                    $row['o_videoaudit_perch_name'] = $o_videoaudit_perch['u_realname'];
                    $row['o_suspend_remarks'] = '';
                    if ($row['o_layer_status'] == '暂停中') {
                        $audit_msg = $OrderHelper->getOneAudit($row['o_id'], 'fao');
                        if (!empty($audit_msg)) $row['o_suspend_remarks'] = $row['o_suspend_remarks'] . "电审【" . $row['o_final_perch_name'] . "】于" . $audit_msg['a_dateline'] . "暂停审核,原因为:\n" . $audit_msg['a_remarks'] . "\n";
                    }
                    if ($row['o_trial_status'] == '暂停中') {
                        $audit_msg = $OrderHelper->getOneAudit($row['o_id'], 'fto');
                        if (!empty($audit_msg)) $row['o_suspend_remarks'] = $row['o_suspend_remarks'] . "资审【" . $row['o_perch_name'] . "】于" . $audit_msg['a_dateline'] . "暂停审核,原因为:\n" . $audit_msg['a_remarks'] . "\n";
                    }
                    if ($row['o_videoaudit_status'] == '暂停中') {
                        $audit_msg = $OrderHelper->getOneAudit($row['o_id'], 'fvo');
                        if (!empty($audit_msg)) $row['o_suspend_remarks'] = $row['o_suspend_remarks'] . "视频审" . $row['o_videoaudit_perch_name'] . "】于" . $audit_msg['a_dateline'] . "暂停审核,原因为:\n" . $audit_msg['a_remarks'] . "\n";
                    }
                    $row['o_suspend_remarks'] = $row['o_suspend_remarks'] . "如需继续审核请及时使用“订单申请修改”功能！";
                    $json_data['rows'][] = $row;
                }
            echo json_encode($json_data);
        }
    } else {
        echo $this->GetView('orderedit_index.php');
    }
} else {
    switch($this->Param['option']){
        case 'list':
            $oid = empty($this->Param['oid']) ? ErrorMsg::Debug('参数错误') : $this->Param['oid'];
            //检测订单是否为权限允许内操作
            $order_check = $OrderHelper->checkOrderper($oid, $this->UserConfig['Id']);
            if(!$order_check['success'])                ErrorMsg::Debug ('无权操作');
            $page = isset($this->Param['page']) ? $this->Param['page'] : 1;
            $rp = isset($this->Param['rp']) ? $this->Param['rp'] : PAGE_SIZE;
            $where = array(
                '`up_oid` = ?' => $oid
            );
            $data = $OrderHelper->omList($where, $page, $rp, $this->Param);
            $json_data = array('total' => $data['count'], 'list' => array(), 'page' => $page, 'pagetotal' => $data['pagetotal']);
            if (!empty($data['all'])){
                foreach ($data['all'] AS $rowNum => $row) {
                    $row['up_info'] = unserialize($row['up_info']);
                    if($row['up_info']['base'] == 'order_attachment'){
                        $row['up_info']['now_val'] = '<a href="javascript:;" class="J-order-upattach" data-id="'.$row['up_id'].'">查看</a>';
                    }
                    $row['up_time'] = date('Y-m-d H:i:s');
                    $json_data['list'][] = $row;
                }
            }
            ErrorMsg::Debug($json_data, true);
            break;
        case 'ajaxkey':
            $oid = empty($this->Param['oid']) ? ErrorMsg::Debug('订单ID值错误') : $this->Param['oid'];
            //检测订单是否为权限允许内操作
            $order_check = $OrderHelper->checkOrderper($oid, $this->Param['uid']);
            if(!$order_check['success'])                ErrorMsg::Debug ('无权操作');
            
            $base = $this->Param['base'];
            $next = $this->Param['next'];
            $start = $this->Param['start'];
            $params = array(
                'oid' => $oid,
                'start' => $start,
                'base' => $base,
                'next' => $next
            );
            $data = $OrderHelper->ajaxMinfo($params);
            echo json_encode($data);
            break;
        case 'sign':
            $oid = empty($this->Param['oid']) ? ErrorMsg::Debug('参数错误') : $this->Param['oid'];
            //检测订单是否为权限允许内操作
            $order_check = $OrderHelper->checkOrderper($oid, $this->UserConfig['Id']);
            if(!$order_check['success'])                ErrorMsg::Debug ('无权操作');
            $msg = $this->LoadModel('Public') ->getOneRow('order_oldinfo','*',array('`o_id` = ?' => $oid));
            $order = $OrderHelper->orderInfo($oid);
            $this->Assign('order',$order);
            $this->Assign('msg',$msg);
            $this->Assign('oid',$oid);
            echo $this->GetView('orderedit_sign.php'); 
            break;
        case 'signsave'://保存
            $data['oid'] = empty($this->Param['oid']) ? ErrorMsg::Debug('订单ID值错误') : $this->Param['oid'];
            $data['base'] = empty($this->Param['base']) ? ErrorMsg::Debug('基础修改参数错误') : $this->Param['base'];
            $data['next'] = empty($this->Param['next']) ? ErrorMsg::Debug('基础修改参数错误') : $this->Param['next'];
            $data['start'] = empty($this->Param['start']) ? ErrorMsg::Debug('选择修改申请要求人') : $this->Param['start'];
            $data['base_val'] = is_null($this->Param['base_val']) ? ErrorMsg::Debug('原值错误') : $this->Param['base_val'];
            if($this->Param['base'] =='order_attachment'){
                if(isset($this->Param['old'])){
                    $old = implode(',',$this->Param['old']);
                    $data['now_val'] = str_replace('http://crm.app.qiche100.com','',$this->Param['now_val'].$old);
                }else{
                    $data['now_val'] = empty($this->Param['now_val']) ? ErrorMsg::Debug('请上传面签材料') : $this->Param['now_val'];
                }
            }else{
                $data['now_val'] = empty($this->Param['now_val']) ? ErrorMsg::Debug('现值错误') : $this->Param['now_val'];
            }
            //检测订单是否为权限允许内操作
            $order_check = $OrderHelper->checkOrderper($data['oid'], $this->Param['uid']);
            if(!$order_check['success'])                ErrorMsg::Debug ('无权操作');
            $do = $OrderHelper->orderupCheck($data);
            if($do['success'] == true){
            $data['up_oid'] = $data['oid'];
            $data['up_uid'] = $this->UserConfig['Id'];
            $data['up_type'] = $data['start'];
            $data['up_uname'] = $member_info['u_realname'];
            $data['up_time'] = NOW_TIME;
            $data['up_reason'] = $this->Param['up_reason'];
            $result = $OrderHelper->orderupSave($data);
            if($result['success'] == true){
                $MemberHelper->saveLog($this->UserConfig, '申请修改订单提交', json_encode($this->Param), $this->Controller.'_'.$this->Action);
                ErrorMsg::Debug('保存成功', true);
            }else{                
                ErrorMsg::Debug($result['msg']);
            }
            }else{
                ErrorMsg::Debug($do['msg']);
            }
            break;
        case 'remove'://撤销
            $up_id = empty($this->Param['up_id']) ? ErrorMsg::Debug('ID值错误') : $this->Param['up_id'];
            $result = $OrderHelper->orderupUpdate(array(
                'up_status' => '作废'
            ),$up_id);
            if($result['success'] == true){
                $MemberHelper->saveLog($this->UserConfig, '作废修改订单', json_encode($this->Param), $this->Controller.'_'.$this->Action);
                ErrorMsg::Debug('保存成功', true);
            }else{                
                ErrorMsg::Debug($result['msg']);
            }
            break;
        case 'audit':
            $up_id = empty($this->Param['up_id']) ? ErrorMsg::Debug('ID值错误') : $this->Param['up_id'];
            $data = $this->LoadModel('Public')->getOneRow('order_up','*',array('`up_id` = ?' => $up_id));
            $this->Assign('data', $data);
            $this->Assign('up_id', $up_id);
            echo $this->GetView('orderedit_audit.php'); 
            break;
        case 'auditsave':
            $up_id = empty($this->Param['up_id']) ? ErrorMsg::Debug('ID值错误') : $this->Param['up_id'];
            $this->Param['up_vf_uid'] = $this->UserConfig['Id'];
            $this->Param['up_vf_uname'] = $member_info['u_realname'];
            $this->Param['up_vf_time'] = NOW_TIME;
            $result = $OrderHelper->orderUp($this->Param);
            if($result['success'] == true){
                $url = false;
                if($member_info['u_role'] == 8 && $result['a_oid']>0)
                    $url = '/order/faoaudit/option/faoinfo?oid='.$result['a_oid'];
                if($member_info['u_role'] == 7 && $result['t_oid']>0)
                    $url = '/order/ftoaudit/option/troinfo?oid='.$result['t_oid'];
                $MemberHelper->saveLog($this->UserConfig, '订单修改申请审核', json_encode($this->Param), $this->Controller.'_'.$this->Action);
                ErrorMsg::Debug($msg, true, $url);
            }else{                
                ErrorMsg::Debug($result['msg']);
            }
            break;
        case 'attachment_view':
            $up_id = empty($this->Param['up_id']) ? ErrorMsg::Debug('ID值错误') : $this->Param['up_id'];
            $up_row = $OrderHelper->orderupRow(array('`up_id` = ?' => $up_id));
            $up_row['up_info'] = unserialize($up_row['up_info']);
            if($up_row['up_info']['base'] == 'order_attachment'){
                $up_row['up_info']['now_val'] = explode(',', $up_row['up_info']['now_val']);
                for($i = 0;$i<count($up_row['up_info']['now_val']);$i++){
                    $up_row['up_info']['now_val'][$i] = str_replace('http://crm.app.qiche100.com', '', $up_row['up_info']['now_val'][$i]);
                }
                $old_msg = explode(',',$up_row['up_info']['base_val']);
                $new_img = array_diff($up_row['up_info']['now_val'],$old_msg);
                $remove_img = array_diff($old_msg,$up_row['up_info']['now_val']);
            }
            if(!empty($up_row['up_info']['now_val'])){
                if($up_row['up_id']>24713){
                    foreach ($up_row['up_info']['now_val'] as $k => $v){
                        if(!empty($v)){
                            $up_row['up_info']['now_val'][$k] = StringCode::doUrlimage($v);
                        }
                    }
                    if($up_row['up_status'] =='申请中' || $up_row['up_status'] =='通过'){
                        foreach ($remove_img as $key => $value) {
                            if(!empty($value)){
                                $up_row['remove_img'][$key] = StringCode::doUrlimage($value);
                            }
                        }
                        foreach ($new_img as $x => $y) {
                            if(!empty($y)){
                                $up_row['new_img'][$x] = StringCode::doUrlimage($y);
                            }
                        }
                    }
                }else{
                    $new_img = ($up_row['up_status'] =='申请中' || $up_row['up_status'] =='通过')?$up_row['up_info']['now_val']:'';
                    $up_row['up_info']['now_val'] = $up_row['up_status'] =='申请中'? array_merge($up_row['up_info']['now_val'],$old_msg):'';
                    if($up_row['up_status'] =='申请中'){
                        foreach ($up_row['up_info']['now_val'] as $k => $v){
                            if(!empty($v)){
                                $up_row['up_info']['now_val'][$k] = StringCode::doUrlimage($v);
                            }
                        }
                    }
                    foreach ($new_img as $x => $y) {
                        if(!empty($y)){
                            $up_row['new_img'][$x] = StringCode::doUrlimage($y);
                        }
                    }
                }
            }
            $this->Assign('data', $up_row);
            echo $this->GetView('orderedit_attachview.php'); 
            break;
        case 'changetype':
            $model = $this->LoadModel('Public');
            $up_id = empty($this->Param['up_id']) ? ErrorMsg::Debug('ID值错误') : $this->Param['up_id'];
            $msg = $model->getOneRow('order_up','*',array('`up_id` = ?' => $up_id));
            if(!empty($msg)){
                if ($msg['up_type'] =='资审'){
                    $model->update('order_up',array('up_type' =>'电审'),array('`up_id` = ?' => $up_id));
                }else{
                    $model->update('order_up',array('up_type' =>'资审'),array('`up_id` = ?' => $up_id));
            }
                ErrorMsg::Debug('操作成功', true);
            }else{
                ErrorMsg::Debug('操作失败');
            }
            break;
        default :
    }
}